package com.deng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.deng.entity.Post;
import com.deng.search.model.PostDocument;
import com.deng.search.mq.PostMqIndexMessage;
import com.deng.search.repository.PostRepository;
import com.deng.service.PostService;
import com.deng.service.SearchService;
import com.deng.vo.PostVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class SearchServiceImpl implements SearchService {
    @Autowired
    PostRepository postRepository;
    @Autowired
    ModelMapper modelMapper;
    @Autowired
    PostService postService;
    @Override
    public IPage search(Page page, String keyWord) {
        Long current=page.getCurrent()-1;
        Long size=page.getSize();
        //分页信息mybatis plus的page转jpa的page
        Pageable pageable= PageRequest.of(current.intValue(),size.intValue());
        //搜索es得到结果
        MultiMatchQueryBuilder multiMatchQueryBuilder=QueryBuilders.multiMatchQuery(keyWord,"title","authorName","categoryName");
        org.springframework.data.domain.Page<PostDocument> docments = postRepository.search(multiMatchQueryBuilder, pageable);
        //将结果从jpa的pageData转换成mybatis plus的pageData
        IPage pageData=new Page(page.getCurrent(),page.getSize(),docments.getTotalElements());
        pageData.setRecords(docments.getContent());
        return pageData;
    }

    @Override
    public int initEsData(List<PostVo> records) {
        if(records==null||records.isEmpty()){
            return 0;
        }
        List<PostDocument> documents=new ArrayList<>();
        //将vo实体转换成document实体，来将数据存入Es
        for(PostVo vo:records){
            PostDocument postDocument=modelMapper.map(vo,PostDocument.class);
            documents.add(postDocument);
        }
        postRepository.saveAll(documents);
        return documents.size();
    }

    @Override
    public void createOrUpdateIndex(PostMqIndexMessage message) {
        Long postId=message.getPostId();
        PostVo postVo=postService.selectOnePost(new QueryWrapper<Post>().eq("id",postId));
        PostDocument postDocument=modelMapper.map(postVo,PostDocument.class);
        postRepository.save(postDocument);
        log.info("es索引更新成功",postDocument.toString());

    }

    @Override
    public void removeIndex(PostMqIndexMessage message) {
        Long postId=message.getPostId();
        postRepository.deleteById(postId);
        log.info("es索引删除成功",message.toString());

    }
}
